import { createStore, applyMiddleware, compose } from 'redux'
import thunkMiddleware from 'redux-thunk'
import { persistState } from 'redux-devtools'
import { combineReducers } from 'redux-immutablejs'
import Immutable from 'immutable'


import rootReducer from '../reducers'
import DevTools from '../containers/DevTools'


const finalCreateStore = compose(
  applyMiddleware(thunkMiddleware),
  DevTools.instrument(),
  persistState(
    window.location.href.match(
      /[?&]debug_session=([^&]+)\b/
    )
  )
)(createStore);

const state = Immutable.fromJS({})
const store = rootReducer(state)

export default function configureStore(store) {
  const st = finalCreateStore(rootReducer, store);

  if (module.hot) {
    module.hot.accept('../reducers', () =>
      st.replaceReducer(require('../reducers').default)
    );
  }

  return st;
}

